<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="所属支行" prop="branchId">
        <el-select
          v-model="dataForm.branchId"
          filterable
          placeholder="请输入关键词">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="工号" prop="jobNumber">
        <el-input v-model="dataForm.jobNumber" :disabled="disabled" placeholder="工号"></el-input>
      </el-form-item>
      <el-form-item label="管辖区域" prop="jurisdiction">
        <el-input v-model="dataForm.jurisdiction" :disabled="disabled" placeholder="管辖区域"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="名片" prop="businessCard">
            <el-img v-model="dataForm.businessCard" :disabled="disabled"></el-img>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="头像" prop="headImgUrl">
            <el-img v-model="dataForm.headImgUrl" :disabled="disabled"></el-img>
          </el-form-item>
        </el-col>
      </el-row>



    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: 0,
          branchId: '',
          name: '',
          jobNumber: '',
          jurisdiction: '',
          businessCard: '',
          headImgUrl: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ],
          jobNumber: [
            {required: true, message: '工号不能为空', trigger: 'blur'}
          ],
          jurisdiction: [
            {required: true, message: '管辖区域不能为空', trigger: 'blur'}
          ],
          branchId: [
            {required: true, message: '请选择所属支行', trigger: 'blur'}
          ],
          businessCard: [
            {required: true, message: '请上传名片', trigger: 'blur'}
          ],
          headImgUrl: [
            {required: true, message: '请上传头像', trigger: 'blur'}
          ]
        },
        loading:false,
        options:[],
      }
    },
    methods: {
      init (id,branchName, disabled) {
        this.url = this.$http.BASE_URL + `/sys/oss/upload?token=${this.$cookie.get('token')}`
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.dataForm.branchName = branchName
        this.visible = true
        this.getBranchList()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/branchclerk/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.branchclerk
              }
            })
          }
        })

      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mall/branchclerk/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      },
      getBranchList (){
        this.$http({
          url: '/mall/branch/queryAll',
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.list
          }
        })
      },
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          this.$http({
            url: '/mall/branch/queryAll',
            method: 'get',
            params: {
              'name': query
            }
          }).then(({data}) => {
            this.loading = false;
            if (data && data.code === 0) {
              this.options = data.list
            }
          })


        } else {
          this.options = [];
        }
      }
    }
  }
</script>
